package net.vkits.controller;

import net.vkits.auth.application.RoleApplicationService;
import net.vkits.auth.domain.model.Role;
import net.vkits.common.Utils;
import net.vkits.common.dto.RoleDTO;
import net.vkits.common.dto.RolePermissionDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ethan-liu on 16/5/8.
 */
@Controller
@RequestMapping("/roles")
public class RoleController {
    private static final Logger log = LoggerFactory.getLogger(RoleController.class);

    @Resource
    private RoleApplicationService roleApplicationService;

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView rolesList() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "system");
        modelMap.put("sidebar", "roles");
        modelMap.put("currentUser",username);
        return new ModelAndView("/roles", modelMap);
    }

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Page<Role> getRoles(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable) {
        Page<Role> roles = roleApplicationService.getRoleList(pageable);
        log.info("size is: {}", roles.getTotalElements());
        return roles;
    }

    @RequiresPermissions("system:sys_role:list")
    @RequestMapping(value = "/list/all",method = RequestMethod.GET)
    @ResponseBody
    public List<Role> getRolesAll() {
        List<Role> roles = roleApplicationService.getRoleAll();
        log.info("size is: {}", roles.size());
        return roles;
    }

    @RequiresPermissions("system:sys_role:add")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String addRole(@RequestBody RoleDTO role) {
        log.info("add Role: {}", role);
        roleApplicationService.addRole(role.getName(), role.getDescription());
        return "done";
    }

    @RequiresPermissions("system:sys_role:del")
    @RequestMapping(method = RequestMethod.DELETE)
    @ResponseBody
    public String delRole(@RequestBody RoleDTO role) {
        log.info("del Role: {}", role);
        roleApplicationService.delRole(role.getId());
        return "done";
    }

    @RequiresPermissions("system:sys_role:modify")
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public String assignRolePermission(@RequestBody RolePermissionDTO rolePermissionDTO) {
        log.info("assignRolePermission: {}", rolePermissionDTO);
        roleApplicationService.assignPermission(rolePermissionDTO.getName(), rolePermissionDTO.getPermissionIds());
        return "done";
    }
}
